package com.fengke.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.fengke.dao.model.OrderItemModel;
import com.fengke.dao.model.OrderModel;
import com.fengke.dao.model.PaymentModel;
import com.fengke.dao.model.Sn;
import com.fengke.dao.order.OrderManager;
import com.fengke.dao.sn.SnManager;
import com.fengke.service.OrderService;
import com.fengke.service.vo.MemberVO;
import com.fengke.service.vo.OrderVO;
import com.fengke.service.vo.PaymentVO;

public class OrderServiceImpl implements OrderService {
	private OrderManager orderManager;
	private SnManager snManager;

	public Integer researchOrderListSize(OrderVO orderVO) throws Exception {
		if(orderVO.getOrderModel() == null) {
			orderVO.setOrderModel(new OrderModel());
		}
		return orderManager.researchOrderListSize(orderVO.getOrderModel());
	}
	public List<OrderVO> researchOrderList(OrderVO orderVO, int offset, int limit) throws Exception {
		if(orderVO.getOrderModel() == null) {
			orderVO.setOrderModel(new OrderModel());
		}
		List<OrderModel> orderModelList = orderManager.researchOrderList(orderVO.getOrderModel(), offset, limit);
		if(orderModelList != null && orderModelList.size() > 0) {
			List<OrderVO> orderVOList = new ArrayList<OrderVO>();
			for(OrderModel model : orderModelList) {
				OrderVO vo = new OrderVO();
				vo.setOrderModel(model);
				// 获取Item信息
				OrderItemModel orderItem = new OrderItemModel();
				orderItem.setOrder(model);
				List<OrderItemModel> orderItemList = orderManager.researchOrderItemListByOrderId(orderItem);
				if(orderItemList != null) {
					vo.setOrderItemModelList(orderItemList);
				}
				orderVOList.add(vo);
			}
			return orderVOList;
		}
		return null;
	}
	public OrderVO researchOrderById(OrderVO orderVO) throws Exception {
		if(orderVO == null || orderVO.getOrderModel() == null || (orderVO.getOrderModel().getId() == null && orderVO.getOrderModel().getSn() == null)) {
			return null;
		}
		// 获取订单信息
		OrderModel orderModel = orderManager.researchOrderById(orderVO.getOrderModel());
		if(orderModel != null && orderModel.getId() != null) {
			orderVO.setOrderModel(orderModel);
			// TODO member 信息
			
			// 获取Item信息
			OrderItemModel orderItem = new OrderItemModel();
			orderItem.setOrder(orderModel);
			List<OrderItemModel> orderItemList = orderManager.researchOrderItemListByOrderId(orderItem);
			if(orderItemList != null) {
				orderVO.setOrderItemModelList(orderItemList);
			}
			return orderVO;
		}
		return null;
	}
	public Integer researchPaymentVOListSize(PaymentVO paymentVO) throws Exception {
		if(paymentVO.getPaymentModel() == null) {
			paymentVO.setPaymentModel(new PaymentModel());
		}
		return orderManager.researchPayMentListSize(paymentVO.getPaymentModel());
	}
	public List<PaymentVO> researchPaymentVOList(PaymentVO paymentVO, int offset, int limit) throws Exception {
		if(paymentVO.getPaymentModel() == null) {
			paymentVO.setPaymentModel(new PaymentModel());
		}
		List<PaymentModel> paymentModelList = orderManager.researchPaymentList(paymentVO.getPaymentModel(), offset, limit);
		if(paymentModelList != null && paymentModelList.size() > 0) {
			List<PaymentVO> paymentVOList = new ArrayList<PaymentVO>();
			for(PaymentModel model : paymentModelList) {
				PaymentVO vo = new PaymentVO();
				vo.setPaymentModel(model);
				paymentVOList.add(vo);
			}
			return paymentVOList;
		}
		return null;
	}
	public PaymentVO researchPaymentVOById(PaymentVO paymentVO) throws Exception {
		PaymentModel paymentModel = orderManager.researchPaymentById(paymentVO.getPaymentModel());
		if(paymentModel != null) {
			paymentVO.setPaymentModel(paymentModel);
			return paymentVO;
		}
		return null;
	}
	@Override
	public OrderVO createOrder(OrderVO orderVO) throws Exception {
		orderVO.getOrderModel().setSn(snManager.generate(Sn.Type.order));
		orderVO.getOrderModel().setId(orderManager.createOrder(orderVO.getOrderModel()));
		for(OrderItemModel orderItem : orderVO.getOrderItemModelList()) {
			orderItem.setSn(snManager.generate(Sn.Type.order));
			orderItem.setOrder(orderVO.getOrderModel());
			orderItem.setId(orderManager.createOrderItem(orderItem));
		}
		return orderVO;
	}
	@Override
	public PaymentVO createPayment(PaymentVO paymentVO) throws Exception {
		paymentVO.getPaymentModel().setSn(snManager.generate(Sn.Type.payment));
		paymentVO.getPaymentModel().setId(orderManager.createPayment(paymentVO.getPaymentModel()));
		return paymentVO;
	}
	@Override
	public void updateOrderVOPaymentStatus(OrderVO orderVO) throws Exception {
		orderManager.updateOrderModelPaymentStatus(orderVO.getOrderModel());
	}
	@Override
	public void updatePaymentVOStatus(PaymentVO paymentVO) throws Exception {
		orderManager.updatePaymentModelStatus(paymentVO.getPaymentModel());
	}
	/**
	 * 更新超时未支付订单
	 */
	@Override
	public void updateTimeoutNopaidOrder() throws Exception {
		OrderModel orderModel = new OrderModel();
		orderModel.setOrderStatus(OrderModel.OrderStatus.cancelled);
		orderModel.setPaymentStatus(OrderModel.PaymentStatus.unpaid);
		orderModel.setExpire(new Date());
		orderModel.setIsEffective(false);
		orderManager.updateTimeoutNopaidOrder(orderModel);
	}
	@Override
	public MemberVO researchMemberOrders(MemberVO memberVO) throws Exception {
		List<OrderModel> orderList = null;
		// 待支付
		/*
		 * root.get("paymentStatus"), PaymentStatus.unpaid)
		 * root.get("orderStatus"), OrderStatus.unconfirmed)
		 */
		List<OrderVO> unPaidOrderList = new ArrayList<OrderVO>();
		OrderModel orderModel = new OrderModel();
		orderModel.setMember(memberVO.getMemberModel());
		orderModel.setPaymentStatus(OrderModel.PaymentStatus.unpaid);
		orderModel.setOrderStatus(OrderModel.OrderStatus.unconfirmed);
		orderModel.setIsEffective(true);
		orderList = orderManager.researchOrderList(orderModel, 0, Integer.MAX_VALUE);
		if(orderList != null && orderList.size() > 0) {
			for(OrderModel order : orderList) {
				OrderVO orderVO = new OrderVO();
				orderVO.setOrderModel(order);
				OrderItemModel orderItem = new OrderItemModel();
				orderItem.setOrder(order);
				List<OrderItemModel> orderItemList = orderManager.researchOrderItemListByOrderId(orderItem);
				if(orderItemList != null) {
					orderVO.setOrderItemModelList(orderItemList);
				}
				unPaidOrderList.add(orderVO);
			}
		}
		memberVO.setUnPaidOrderList(unPaidOrderList);
		
		
		orderModel = new OrderModel();
		orderModel.setMember(memberVO.getMemberModel());
		orderModel.setIsEffective(true);
		orderModel.setPaymentStatus(OrderModel.PaymentStatus.paid);
		orderModel.setOrderStatus(OrderModel.OrderStatus.unconfirmed);
		List<OrderModel> orderModelList = orderManager.researchOrderList(orderModel, 0, Integer.MAX_VALUE);
		
		List<OrderVO> paidOrderList = new ArrayList<OrderVO>();
		List<OrderVO> ongoingOrderList = new ArrayList<OrderVO>();
		Date nowDate = new Date();
		for(OrderModel order : orderModelList) {
			OrderVO orderVO = new OrderVO();
			orderVO.setOrderModel(order);
			OrderItemModel orderItem = new OrderItemModel();
			orderItem.setOrder(order);
			List<OrderItemModel> orderItemList = orderManager.researchOrderItemListByOrderId(orderItem);
			if(orderItemList != null) {
				orderVO.setOrderItemModelList(orderItemList);
			}
			if(nowDate.before(order.getGoDate())) {
				paidOrderList.add(orderVO);
			} else {
				ongoingOrderList.add(orderVO);
			}
		}
		// 已支付
		/*
		 * root.get("paymentStatus"), PaymentStatus.paid)
		 * root.get("orderStatus"), OrderStatus.unconfirmed)
		 * greaterThan(root.<Date> get("goDate"), new Date())
		 */
		if(paidOrderList != null && paidOrderList.size() > 0) {
			memberVO.setPaidOrderList(paidOrderList);
		}
		// 进行中
		/*
		 * root.get("paymentStatus"), PaymentStatus.paid)
		 * root.get("orderStatus"), OrderStatus.unconfirmed)
		 * lessThanOrEqualTo(root.<Date> get("goDate"), new Date()
		 */
		if(ongoingOrderList != null && ongoingOrderList.size() > 0) {
			memberVO.setOngoingOrderList(ongoingOrderList);
		}
		// 已完成
		/*
		 * root.get("paymentStatus"), PaymentStatus.paid))
		 * root.get("orderStatus"), OrderStatus.completed))
		 */
		List<OrderVO> completedOrderList = new ArrayList<OrderVO>();
		orderModel = new OrderModel();
		orderModel.setMember(memberVO.getMemberModel());
		orderModel.setPaymentStatus(OrderModel.PaymentStatus.paid);
		orderModel.setOrderStatus(OrderModel.OrderStatus.completed);
		orderModel.setIsEffective(true);
		orderList = orderManager.researchOrderList(orderModel, 0, Integer.MAX_VALUE);
		if(orderList != null && orderList.size() > 0) {
			for(OrderModel order : orderList) {
				OrderVO orderVO = new OrderVO();
				orderVO.setOrderModel(order);
				OrderItemModel orderItem = new OrderItemModel();
				orderItem.setOrder(order);
				List<OrderItemModel> orderItemList = orderManager.researchOrderItemListByOrderId(orderItem);
				if(orderItemList != null) {
					orderVO.setOrderItemModelList(orderItemList);
				}
				completedOrderList.add(orderVO);
			}
		}
		memberVO.setCompletedOrderList(completedOrderList);
		// 已关闭
		/*
		 * root.get("orderStatus"), OrderStatus.cancelled)
		 * root.get("isEffective"), true
		 */
		List<OrderVO> cancellOrderList = new ArrayList<OrderVO>();
		orderModel = new OrderModel();
		orderModel.setMember(memberVO.getMemberModel());
		orderModel.setOrderStatus(OrderModel.OrderStatus.cancelled);
		orderModel.setIsEffective(true);
		orderList = orderManager.researchOrderList(orderModel, 0, Integer.MAX_VALUE);
		if(orderList != null && orderList.size() > 0) {
			for(OrderModel order : orderList) {
				OrderVO orderVO = new OrderVO();
				orderVO.setOrderModel(order);
				OrderItemModel orderItem = new OrderItemModel();
				orderItem.setOrder(order);
				List<OrderItemModel> orderItemList = orderManager.researchOrderItemListByOrderId(orderItem);
				if(orderItemList != null) {
					orderVO.setOrderItemModelList(orderItemList);
				}
				cancellOrderList.add(orderVO);
			}
		}
		memberVO.setCancellOrderList(cancellOrderList);
		
		return memberVO;
	}
	public int deleteOrderByIds(List<String> idList) throws Exception {
		// 级联删除orderItem/payment
		if(idList == null || idList.size() <= 0) {
			return 0;
		}
		orderManager.deleteOrderItemByOrderIds(idList);
		orderManager.deletePaymentByOrderIds(idList);
		orderManager.deleteOrderByIds(idList);
		return 0;
	}
	@Override
	public void updateOrderVOStatus(OrderVO orderVO) throws Exception {
		orderManager.updateOrderModelStatus(orderVO.getOrderModel());
	}
	@Override
	public void associateShow(OrderVO orderVO) throws Exception {
		orderManager.updateAssociateShowStatus(orderVO.getOrderModel());
	}
	
	public OrderManager getOrderManager() {
		return orderManager;
	}
	public void setOrderManager(OrderManager orderManager) {
		this.orderManager = orderManager;
	}
	public SnManager getSnManager() {
		return snManager;
	}
	public void setSnManager(SnManager snManager) {
		this.snManager = snManager;
	}
}
